
# include "dfxCorePlugin.h"

namespace DFX
{    
    CorePlugin::CorePlugin(const String &dllFile)
        : Plugin("Core"),
        mDllFile(dllFile),
        mWindowCreate(NULL),
        mRegisterWindowClass(NULL),
        mUnregisterWindowClass(NULL),
        mProcessEvents(NULL),
        mFileCreate(NULL),
        mThreadCreate(NULL),
        mMutexCreate(NULL),
        mTimerCreate(NULL)
    {
    }

    CorePlugin::~CorePlugin()
    {
        Unload();
    }
        
    DFXRESULT CorePlugin::WindowCreate(const String &name, const String &className, Window **window)
    {
        if (mWindowCreate)
            return mWindowCreate(name, className, window);

        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::RegisterWindowClass(const String &name)
    {
        if (mRegisterWindowClass)
            return mRegisterWindowClass(name);

        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::UnregisterWindowClass(const String &name)
    {
        if (mUnregisterWindowClass)
            return mUnregisterWindowClass(name);

        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::ProcessEvents()
    {
        if (mProcessEvents)
            return mProcessEvents();

        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::FileCreate(const String &fileName, File **file)
    {
        if (mFileCreate)
            return mFileCreate(fileName, file);

        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::ThreadCreate(PRIORITY priority, Thread **t)
    {
        if (mThreadCreate)
            return mThreadCreate(priority, t);
        
        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::MutexCreate(Mutex **m)
    {
        if (mMutexCreate)
            return mMutexCreate(m);

        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::TimerCreate(Timer **t)
    {
        if (mTimerCreate)
            return mTimerCreate(t);

        return DFX_CREATEFAILED;
    }

    DFXRESULT CorePlugin::Load()
    {
        LoadDLL(mDllFile, (void*)&mHandle);

        if (mHandle)
        {
            mWindowCreate           = (WINDOWCREATE)GetFunction("WindowCreate", (void*)&mHandle);
            mRegisterWindowClass    = (REGISTERWINDOWCLASS)GetFunction("RegisterWindowClass", (void*)&mHandle);
            mUnregisterWindowClass  = (UNREGISTERWINDOWCLASS)GetFunction("UnregisterWindowClass", (void*)&mHandle);
            mProcessEvents          = (PROCESSEVENTS)GetFunction("ProcessEvents", (void*)&mHandle);
            mFileCreate             = (FILECREATE)GetFunction("FileCreate", (void*)&mHandle);
            mThreadCreate           = (THREADCREATE)GetFunction("ThreadCreate", (void*)&mHandle);
            mTimerCreate            = (TIMERCREATE)GetFunction("TimerCreate", (void*)&mHandle);

            return DFX_SUCCESS;
        }

        return DFX_LOADFAILED;
    }

    DFXRESULT CorePlugin::Unload()
    {
        if (mHandle)
            UnloadDLL((void*)&mHandle);

        return DFX_SUCCESS;
    }
}